package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DelayMessageConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 封捷
 * @create-date: 2022/9/13 10:09
 */
@Slf4j
@Configuration
public class DelayMessageReceiver {

    // 不要在这里做路由键、交换机的绑定
    @RabbitListener(queues = DelayMessageConfig.QUEUE_DELAY)
    public void receive(String messageContent, Message message, Channel channel) throws IOException {

        // 打印接收到的消息
        log.info("[接收端]" + messageContent + " " + new SimpleDateFormat("hh:mm:ss").format(new Date()));

        // 返回 ACK 消息确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
